import React, {Component } from 'react';
import { Route, Switch } from 'react-router';
import { ConnectedRouter } from 'connected-react-router';
import { connect } from 'react-redux';

import LoginView from '@/views/Login';
import AppView from '@/views/App';

@connect(state => ({
  login: state.login
}))
export default class AppRouter extends Component {
  constructor(props) {
    super(props);
  }
  onEnter(WrappedComponent, prop) {
    if (this.props.login.isLogin) {
      return (
        <WrappedComponent
          {...prop}
        />
      );
    }
    return <LoginView />
  }
  render() {
    const { history } = this.props;
    return (
      <ConnectedRouter history={history}>
        <div>
          <Switch>
            <Route
              exact
              path="/login"
              component={LoginView}
            />
            <Route
              path="/"
              render={prop => this.onEnter(AppView, prop)}
            />
          </Switch>
        </div>
      </ConnectedRouter>
    )
  }
}
